<?php
/*************************************************
*
*    Folder Space Folder Quota Class
*    @filename: folder_space.class.php
*    Php version PHP5.
*    Revision : 27
*    version: 0.2
*    @author: Ersin Güvenç <eguvenc@gmail.com>  (C) 2008.
*    Web: http://develturk.com
*    @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License Version 2.1
*
*/
//catch all error.. you can customize exception class.look at php5 manual.
Class folder_space_Exception extends Exception {
    function __toString(){
    return __CLASS__ . ": [Error]: {$this->getMessage()} [Line]: {$this->getLine()}\n";
    }
}
/**/
Class folder_space {

    //folder path
    private $path = "";
    //static instance.
    static $instance;
    //this object can not copy except getInstance() method.
    private function __clone() {}

    private function __construct(){}
    //lets prevent duplication of memory using by Singleton.
    public static function getInstance()
    {
       if(! (self::$instance instanceof self))
       {
        self::$instance = new self();
       }
       return self::$instance;
    }

    //initalize check disk space
    public function check($path)
    {   //start process...
        $total = $this->get_size($path);
        $result['byte'] = $total;
        $result['filesize'] = $this->file_size($total);
        return $result;
    }

    // find the correct size type..
    public function file_size($size)
    {
    $filesizename = array(" Byte", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Byte';
    }

     /* From PHP.NET */
    function get_size($path){
           if(!is_dir($path))
           throw new folder_space_Exception('Directory not found! '.__FUNCTION__.' error!');
           $dir = opendir($path);
           while($file = readdir($dir))
           {
               $convert = mb_convert_case($file, MB_CASE_LOWER, "UTF-8");
               if(eregi("[^a-z0-9]^[._-]", $convert))
               throw new folder_space_Exception('Directory not found! '.__FUNCTION__.' error!');

               if(is_file($path."/".$file))$size+=filesize($path."/".$file);
               if(is_dir($path."/".$file) && $file!="." && $file !="..")$size +=$this->get_size($path."/".$file);

           }
           closedir($dir);
          return $size;
     }

} //end of the folder_space class


Class percent_bar extends folder_space {

    public $numberOf = "";
    public $totalNumberOf = "";
    public $percent = "";
    public $bar_name = "";

    private $used = "";
    private $filesize = "";
    private $disk_full = false; //if disk full true...
    private $exceed = false; //if disk exceed over the quota exceed true
    /*
    */
    public $maxheight = "120";
    public $maxwidth = "3";    //minumum 3px for ie.

    //static instance.
    static $instance;
    //this object can not copy except getInstance() method.
    private function __clone() {}

    private function __construct(){}
    //lets prevent duplication of memory using by Singleton.
    public static function getInstance()
    {
       if(! (self::$instance instanceof self))
       {
        self::$instance = new self();
       }
       return self::$instance;

    }
    //...set data
    function set_params($data){

        if($data['total'] < $data['used']):
        $this->exceed = true;
        $this->disk_full = false;
        $this->numberOf = $data['total'];

        elseif($data['total'] == $data['used']):
        $this->numberOf = $data['used'];
        $this->disk_full = true;
        $this->exceed = false;
        else:
        $this->numberOf = $data['used'];
        $this->exceed = false;
        $this->disk_full = false;
        endif;

        $this->filesize = $data['filesize'];
        $this->used = $data['used'];
        $this->totalNumberOf = $data['total'];
        $this->maxheight = $data['max_height'];
        $this->maxwidth = $data['max_width'];
        $this->bar_name = $data['bar_name'];
        $this->percent = round(($this->numberOf / $this->totalNumberOf) * 100);
    }

    function usedPercent(){
        return (100-$this->percent);
    }

    //calculate used disk space div height ...
    function used_divHeight(){
        return round(($this->maxheight * $this->percent) / 100);
    }

    //calculate none used disk space div height ...
    function none_used_divHeight(){
        return round(($this->maxheight * $this->usedPercent()) / 100);
    }


    function build_vertical($empty_color,$full_color,$float){
        /*  MAIN DIV  */
        echo "<div class=\"vertical_frame\" style=\" \n";
        echo "float:$float; \n";
        echo "display:block; \n";
        echo "width:200px; \n";  //edit here for extend div
        echo "font-family:verdana; \n";
        echo "color:#999; \n";
        echo "\">";

            ## 100 MB label ##
            echo "<div class=\"vertical_quota_size\" style=\" \n";
            echo "width:100px; \n"; //and set here for extend div
            echo "font-weight:bold; font-size:10px; color:$full_color\n";
            echo "\">";
            if($this->disk_full) { echo "<b>FULL</b>"; }
            elseif($this->exceed){ echo "<b>EXCEED</b>"; }
            echo "</div>";
            ## 100 MB label ##

            echo "<div class=\"vertical_percentbar\" style=\" \n";
            echo "overflow: hidden;";
            echo "width:".$this->maxwidth."px; \n";
            echo "height:".$this->maxheight."px; \n";
            echo "float:left; \n";
            echo "margin-right:20px; \n";
            echo "background-color: $empty_color; \n";
            echo "border: 1px solid $full_color; \n";
            echo "\">";

                ## none used space ##
                echo "<div class=\"vertical_none_used\" style=\" ";
                if($this->none_used_divHeight() > 0)  //IE hack.
                echo "width:".$this->maxwidth."px;";
                if($this->none_used_divHeight() > 0)
                echo "height:".$this->none_used_divHeight()."px;";
                echo "\">";

                echo "</div>";
                ## none used space ##

                ## used space ##
                echo "<div class=\"vertical_used\" style=\"height: \n";
                echo $this->used_divHeight();
                echo "px;width:".$this->maxwidth."px;background-color: $full_color; \">";
                echo "</div>";
                ## used space ##

            echo "</div> \n"; //vertical percent_bar end

        /*echo "<div style=\"clear:both;\"></div>";*/		/*unused for joomla*/

        ## used Percent %100 ##
        echo "<div class=\"vertical_num\" style=\"color:$full_color;width:200px; ";
        echo "margin-top:4px;text-align:left;font-size:14px;font-weight:bold;";
        echo "\"> \n";
        echo $this->bar_name." : ".$this->percent." %\n";
        echo "<br/><span style='color:grey;font-weight:normal;font-size:10px;'>(".$this->file_size($this->used)." / ".$this->file_size($this->totalNumberOf).")</span></div> \n";
        ## used Percent %100 ##

        ## used space  ##
/*		echo "<div class=\"vertical_used_num\" style=\"color:$full_color;width:80px; ";
        echo "margin-top:4px;text-align:left;font-size:12px;";
        echo "\"> \n";
        echo $this->file_size($this->used)." kullan&#305;lan";
        echo "</div> \n";
        ## used Percent %100 ##  

        echo "<div style=\"clear:both;\"></div>";

        ## Barname ##
        echo "<div class=\"vertical_bar_name\" style=\"width:80px;text-align:left;font-size:12px;\"> \n";
        echo $this->bar_name."\n";
        echo "</div> \n";
        ## Barname ## */	/*unused for joomla*/

        echo "</div>"; // vertical_frame div end
        /*  MAIN DIV  END*/
    }


    function build_horizontal($empty_color,$full_color,$float){
        /*  MAIN DIV  */
        echo "<div class=\"horizontal_frame\" style=\" \n";
        echo "float:$float; \n";
        echo "display:block; \n"; //version 0.2 ; error fixed.
        echo "width:300px; \n";  //edit here for extend div
        echo "height:80px; \n";
        echo "font-family:verdana; \n";
        echo "color:#999; \n";
        echo "\">";

            ## 100 MB label ##
            echo "<div class=\"horizontal_quota_size\" style=\" \n";
            //echo "width:100px; \n"; //and set here for extend div
            echo "font-weight:bold; font-size:15px; \n";
            echo "\">";
            if($this->disk_full) { echo "<b>FULL</b>"; }
            elseif($this->exceed){ echo "<b>EXCEED</b>"; }
            else{ echo $this->file_size($this->totalNumberOf);}
            echo "</div>";
            ## 100 MB label ##

            echo "<div class=\"horizontal_percentbar\" style=\" \n";
            echo "overflow: hidden;"; //IE height hack.
            echo "width:".$this->maxheight."px; \n";
            echo "height:".$this->maxwidth."px; \n";  //height px version 0.2 fixed.
            echo "color:#999; \n";
            //echo "float:left; \n";
            echo "margin-right:20px; \n";
            echo "background-color: $empty_color; \n";
            echo "border: 1px solid $full_color; \n";
            echo "\">";

                 ## used space ##
                echo "<div class=\"horizontal_used\" style=\"overflow: hidden;float:left;width: \n";
                echo $this->used_divHeight();
                echo "px;height:".$this->maxwidth."px;background-color: $full_color; \"> \n";
                echo "</div>";
                ## used space ##

                ## none used space ##
           /*    echo "<div class=\"horizontal_none_used\" style=\"float:left; ";
                //if($this->none_used_divHeight() > 0)
                //echo "width:".$this->none_used_divHeight()."px;";
                echo "\">";
                echo "</div>"; */
                ## none used space ##
                //echo "<div style=\"clear:left;\"></div>";

            echo "</div> \n"; //horizontal percent_bar end

       /*  echo "<div style=\"clear:both;\"></div>";*/

        ## Percent %100 ##
        echo "<div class=\"horizontal_num\" style=\"color:$full_color; \n";
        echo "width:".$this->maxheight."px;";
        echo "margin-top:4px;text-align:left;font-size:14px;";
        echo "\"> \n";
        echo "<span style=\"font-weight:bold;\">% ".$this->percent."</span>\n";
        echo "<span style=\"font-size:12px;\">".$this->file_size($this->used)." kullan&#305;l&#305;yor";
        echo "</span>";
        echo "</div> \n";
        ##  Percent %100 ##

        echo "<div style=\"clear:both;\"></div>";

        ## barname ##
        echo "<div class=\"horizontal_bar_name\" style=\" \n";
        echo "width:".$this->maxheight."px;";
        echo "text-align:left;font-size:12px;\"> \n";
        echo $this->bar_name."\n";
        echo "</div> \n";
        ## barname ##

        echo "</div>";
    }

} // end of the class...

//folder_space with percent_bar Controller class.
class handle {
/*  */
    function create($data){
      try{
      ///...
      if(is_array($data['dir'])){
        foreach ($data['dir'] as $val){
        $result = folder_space::getInstance()->check($val);
        $total['byte'] += $result['byte'];
        $total['filesize'] += $result['filesize'];
        //print_r($total);
        }
      }else{
      $result = folder_space::getInstance()->check($data['dir']);
      $total['byte'] = $result['byte'];
      $total['filesize'] = $result['filesize'];
      }

      }catch(Exception $e) {echo $e;}

      $data['used'] = $total['byte'];
      $data['filesize'] = $total['filesize'];
      switch ($data['axis']) {
        case 'vertical':
            $bar = percent_bar::getInstance();
            $bar->set_params($data);
            $bar->build_vertical($data['empty_color'],$data['full_color'],$data['float']);
            if($data['var_dump'])
            var_dump($bar);
            break;
        case 'horizontal':
            $bar2 = percent_bar::getInstance();
            $bar2->set_params($data);
            $bar2->build_horizontal($data['empty_color'],$data['full_color'],$data['float']);
            if($data['var_dump'])
            var_dump($bar2);
            break;
      }
    }

} // end of the handle class.


?>